#!coding=utf-8
from __future__ import unicode_literals
from datetime import datetime

from django.db import models
from django.db.models import Q
from django.core.paginator import *


# Create your models here.


class Server(models.Model):
    '''主机表'''
    name = models.CharField(verbose_name=u'主机名', max_length=120)
    memory = models.IntegerField(verbose_name=u'内存')
    generatorroom = models.ForeignKey('GeneratorRoom', verbose_name=u'所在机房', related_name='servers')
    cpu = models.ForeignKey('Cpu', verbose_name=u'CPU型号')
    cpu_count = models.SmallIntegerField(verbose_name=u'CPU个数')
    password = models.CharField(verbose_name=u'密码', max_length=200, blank=True, null=True)
    mysql_password = models.CharField(verbose_name=u'MySQL密码', max_length=200, blank=True, null=True)
    add_time = models.DateField(verbose_name=u'添加时间', default=datetime.now)
    ssh_port = models.IntegerField(verbose_name=u'SSH端口', default=22)
    description = models.TextField(verbose_name=u'备注')

    class Meta:
        verbose_name = u'主机'
        verbose_name_plural = verbose_name
        permissions = (
            ('view_password', u'密码可视'),
        )

    def __str__(self):
        return self.name.encode('utf-8')

    def get_cpu(self):
        return self.cpu.name
    get_cpu.short_description = u'CPU型号'

    def get_disks_list(self):
        return self.disks.all()

    def get_disks(self):
        return '；'.join([u'硬盘名：' + disk.name + u'，挂载目录：' + disk.mount_path + u'，硬盘大小：' + str(disk.size) + 'GB' for disk in self.disks.all()])
    get_disks.short_description = u'硬盘'

    def get_ipaddress(self):
        return '；'.join([u'IP地址：' + ipaddress.ip + u'，IP类型：' + ipaddress.get_type_display() for ipaddress in self.ipaddress.all()])
    get_ipaddress.short_description = u'IP地址'

    def get_vlan_ip(self):
        vlan_ip = []
        ips = self.ipaddress.all()
        for ip in ips:
            if ip.type == 0:
                vlan_ip.append(ip.ip)
        return u'；'.join(vlan_ip)
    get_vlan_ip.short_description = u'内网IP地址'

    def get_wan_ip(self):
        wan_ip = []
        ips = self.ipaddress.all()
        for ip in ips:
            if ip.type == 1:
                wan_ip.append(ip.ip)
        return u'；'.join(wan_ip)
    get_wan_ip.short_description = u'外网IP地址'

    def get_kernels(self):
        return self.cpu_count * self.cpu.kernel
    get_kernels.short_description = u'CPU内核总数'

    def get_ssh_user(self):
        return '；'.join([u'用户名：' + user_ssh.username + '，密码：' + user_ssh.password for user_ssh in self.ssh_users.all()])
    get_kernels.short_description = u'ssh用户'



class Cpu(models.Model):
    '''CPU表'''
    name = models.CharField(verbose_name=u'CPU型号', max_length=50)
    kernel = models.SmallIntegerField(verbose_name=u'内核数')
    frequency = models.CharField(verbose_name=u'主频', max_length=50)

    class Meta:
        verbose_name = u'CPU'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name.encode('utf-8')


class Disk(models.Model):
    '''硬盘表'''
    name = models.CharField(verbose_name=u'硬盘名', max_length=50)
    mount_path = models.CharField(verbose_name=u'挂在目录', max_length=50, blank=True, null=True)
    size = models.IntegerField(verbose_name=u'硬盘大小')
    server = models.ForeignKey('Server', verbose_name=u'所属主机', related_name='disks')
    add_time = models.DateField(verbose_name=u'添加时间', default=datetime.now)

    class Meta:
        verbose_name = u'硬盘'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name.encode('utf-8')

    def get_server(self):
        return self.server.name
    get_server.short_description = u'所属主机'


class GeneratorRoom(models.Model):
    '''机房表'''
    name = models.CharField(verbose_name=u'机房名', max_length=50)
    address = models.CharField(verbose_name=u'机房地址', max_length=200)
    phone_num = models.CharField(verbose_name=u'联系电话', max_length=20)
    add_time = models.DateField(verbose_name=u'添加时间', default=datetime.now)

    class Meta:
        verbose_name = u'机房信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name.encode('utf-8')

    def get_servers(self):
        return ','.join([server.name for server in self.servers.all()])
    get_servers.short_description = u'主机'


class IpAddress(models.Model):
    '''ip地址表'''
    ip = models.GenericIPAddressField(verbose_name=u'IP地址')
    type = models.SmallIntegerField(verbose_name=u'IP地址类型', choices=((0, u'内网地址'), (1, u'外网地址')))
    server = models.ForeignKey('Server', verbose_name=u'所属主机', related_name='ipaddress')
    add_time = models.DateField(verbose_name=u'添加时间', default=datetime.now)

    class Meta:
        verbose_name = u'IP地址'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.ip.encode('utf-8')

    def get_server(self):
        return self.server.name
    get_server.short_description = u'所属主机'



